package com.jxc.action.qtmk;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VRemindWarn;
import com.jxc.model.vo.VUser;
import com.jxc.service.RemindWarnServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/qtmk")
@Action(value = "remindWarnAction", results = { @Result(name = "tab", location = "/remindwarn/tabRemindWarn.jsp") })
public class RemindWarnAction extends BaseAction implements ModelDriven<VRemindWarn> {
	private final static Logger logger = Logger.getLogger(RemindWarnAction.class);

	private VRemindWarn model = new VRemindWarn();

	@Autowired
	private UserServiceI userService;
	@Autowired
	private RemindWarnServiceI remindWarnService;

	public String noAuth_tabRemindWarn() {
		List<VRemindWarn> remindWarns = new ArrayList<VRemindWarn>();

		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		VRemindWarn param = new VRemindWarn();
		param.setIsDone(false);
		if (vuser.getReid() != null) {
			param.setRid(vuser.getReid());
		} else {
			param.setUid(vuser.getId());
		}
		remindWarns.addAll(remindWarnService.combobox(param));

		this.getRequest().setAttribute("RemindWarns", remindWarns);
		return "tab";
	}

	public void noAuth_confirm() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());

		Json j = new Json();
		try {
			List<VRemindWarn> vrws = this.remindWarnService.combobox(model);
			VRemindWarn vrw = vrws.get(0);
			vrw.setIsDone(true);
			vrw.setConfirmTime(new Date());
			vrw.setCuid(vuser.getId());
			this.remindWarnService.update(vrw);

			j.setMsg("确认成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("确认失败");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	@Override
	public VRemindWarn getModel() {
		return model;
	}

}
